package moodish.client;

import java.util.ArrayList;

import javax.swing.ImageIcon;


/**
 * This class represents the concept of a User with is Name, Mood, ImageIcon and a list of Friends associated.
 * 
 * @author es1-2013-group2
 *
 */
public class User implements Comparable<User>{

	private String name;
	private Mood mood;
	private ImageIcon imageIcon;
	private ArrayList<User> friends;
	private boolean connected;

	/**
	 * The constructor receives the Name and Mood of a User.
	 * 
	 * @param name Name of the User.
	 * @param mood Mood of the User.
	 */
	public User(String name, Mood mood){
		this.friends = new ArrayList<User>();
		this.name = name;
		this.mood = mood;
		this.imageIcon = mood.getImageIcon();
		this.connected = false;
	}

	/**
	 * Sets the mood of the User.
	 * 
	 * @param mood Mood of the User.
	 */
	public void setMood(Mood mood){
		this.mood = mood;
		this.imageIcon = mood.getImageIcon();
	}

	/**
	 * Returns the Name of the User.
	 * 
	 * @return String Name of the User.
	 */
	public String getNickname() {
		return name;
	}

	/**
	 * Returns the ImageIcon of the User.
	 * 
	 * @return ImageIcon Image of the User.
	 */
	public ImageIcon getImageIcon() {
		return imageIcon;
	}

	/**
	 * Returns the Mood of a User.
	 * 	
	 * @return Mood Mood of a User.
	 */
	public Mood getMood(){
		return mood;
	}
	
	/**
	 * Compare the Mood of two Users and returns is order.
	 * 
	 * @param otherFriend Another User to compare Mood.
	 * @return int Returns 0 if the Ordinal is the same in two users.
	 */
	public int compareTo(User otherFriend){
		return mood.ordinal()-otherFriend.getMood().ordinal();
	}

	/**
	 * Add a Friend to this User list of friends (Users).
	 * 
	 * @param friend User friend to add to the list.
	 */
	public void addFriend(User friend){
		friends.add(friend);
	}

	/**
	 * Removes a friend User from this Users list of friends.
	 * 
	 * @param friend User friend to remove from the list.
	 */
	public void removeFriend(User friend){
		friends.remove(friend);
	}
	
	public void removeFriend(String nickname){
		
		User user = null;
		for(User friend: friends){
			if(friend.getNickname().equals(nickname))
				user = friend;
		}
		friends.remove(user);
	}

	/**
	 * Returns the User's list of friends.
	 * 
	 * @return ArrayList of this Users friends.
	 */
	public ArrayList<User> getFriends(){
		return friends;
	}

	/**
	 * Removes all friends from the User.
	 * 
	 */
	public void removeAllFriends() {
		this.friends = new ArrayList<User>();			
	}
	
	public void connect(){
		connected = true;
	}
	
	public void disconnect(){
		connected = false;
	}
	
	public boolean isConnected(){
		return connected;
	}

}